<%@ page language="java" pageEncoding="UTF-8" %>
<%@page import="global.Constants"%>
<%@page import="util.ServletHelp"%>
<html>
<head>
	<%
		String baseUrl = request.getContextPath();
		String userId = request.getParameter("userId");
	%>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script type="text/javascript">
		$(document).ready(function(){
		    //角色勾选的变量
		    var checkid = '';;
		    var checktext = '';;
		    //部门的变量
		    var deptid = '';;
		    var depttext = '';
			// 工具栏
		    var toolbar = new Ext.Toolbar({
		    	renderTo: 'editUserToolBarDiv',
		    	items: [
				    new Ext.Button({
					    id: 'editUser-save-button',
						text: '保存',
						iconCls: 'tick'
					}),
					new Ext.Button({
					    id: 'editUser-cancel-button',
						text: '取消',
						iconCls: 'cancel'
					})
				]
			});
			


	     	// 性别数据源
	        var sexStore = new Ext.data.ArrayStore({  
	            fields : ['sex', 'sexName'],  
	            data : <%=ServletHelp.getArrayFromMap(Constants.SEX_MAP, null)%>,
	            sortInfo: {field: "sex", direction: "ASC"}
	        });
	        
	             
		   //给用户添加角色    
		   var comboWithTooltipRole_edit = new Ext.form.ComboBox({
		    id:'cr',
		    fieldLabel: '角色',
            store:new Ext.data.SimpleStore({fields:[],data:[[]]}),  
            editable:false,  
            mode: 'local',  
            triggerAction:'all',  
            maxHeight: 200,  
			allowBlank:false,
            tpl: '<div style="height:200px"><div id="treeRole_edit"></div></div>',  
            onSelect:Ext.emptyFn,
            emptyText:'请选择...',
            anchor:'95%',
            //applyTo: 'comboxtreeRole'  
            renderTo: Ext.getBody()
        });
   
        comboWithTooltipRole_edit.render('comboWithTooltipRole_edit');  
        
        var treeRole_edit = new Ext.tree.TreePanel({  
                border:false,  
                autoScroll:true,
                animate:true,
                autoWidth:true,
                autoHeight:true,
                enableDD:true,
                containerScroll: true, 
				rootVisible: true,
                loader: new Ext.tree.TreeLoader({
                    baseParams: {userId:'<%=userId %>'},
                    dataUrl:'${pageContext.request.contextPath}/getRole.action',
                    listeners : {
					beforeload : function(treeLoader, node) {
						this.baseParams.parentId = node.id
						this.baseParams.text = node.text
						this.baseParams.leaf = node.leaf
					}
				}           
                })
            });
     
        var rootRole_edit = new Ext.tree.AsyncTreeNode({
                id:'-1',                      //节点id
                text: '角色',                  //节点名称
                draggable:true,                  //是否支持拖动
                nodeType: 'async', 
                leaf:false,
                checked : false
        });
          

			treeRole_edit.setRootNode(rootRole_edit);
            comboWithTooltipRole_edit.on('expand',function(){  
            treeRole_edit.render('treeRole_edit');  
            //加载所有的子节点
            //tree.expandAll(); 
            });  
            
        
        treeRole_edit.on('checkchange', function(node, checked) {
                        node.expand();
                        node.attributes.checked = checked;
                        node.eachChild(function(child) {
                                    child.ui.toggleCheck(checked);
                                    child.attributes.checked = checked;
                                    child.fireEvent('checkchange', child,
                                            checked);
                               });
                            //这里是遍历勾选的角色
                            var b = treeRole_edit.getChecked();
                            checkid = new Array;// 存放选中id的数组
                            checktext = new Array;// 存放选中text的数组
                            for (var i = 0; i < b.length; i++) {
                                checktext.push(b[i].text);// 添加text到数组
                                checkid.push(b[i].id);// 添加id到数组
                                
                            }
                       comboWithTooltipRole_edit.setValue(checktext.toString());
                    }, treeRole_edit);
            
            //给用户添加部门
            var comboWithTooltipDept_edit = new Ext.form.ComboBox({
			    id:'cd',
			    fieldLabel: '部门',
	            store:new Ext.data.SimpleStore({fields:[],data:[[]]}),  
	            editable:false,  
	            mode: 'local',  
	            triggerAction:'all',  
	            maxHeight: 200,
				allowBlank:false,  
	            tpl: '<div style="height:200px"><div id="treeDept_edit"></div></div>',  
	            onSelect:Ext.emptyFn,
	            emptyText:'请选择...',
	            anchor:'95%',
	            // applyTo: 'comboxtreeDept'  
	            renderTo: Ext.getBody()
           });
        
        comboWithTooltipDept_edit.render('comboWithTooltipDept_edit');  
        
        var treeDept_edit = new Ext.tree.TreePanel({  
                border:false,  
                autoScroll:true,
                animate:true,
                autoWidth:true,
                autoHeight:true,
                enableDD:true,
                containerScroll: true, 
				rootVisible: true,
                loader: new Ext.tree.TreeLoader({
                    dataUrl:'${pageContext.request.contextPath}/department_getDepartmentTree.action',
                    listeners : {
					beforeload : function(treeLoader, node) {
						this.baseParams.parentId = node.id
						this.baseParams.text = node.text
						this.baseParams.leaf = node.leaf
					}
				}           
                })
            });
            
        var rootDept_edit = new Ext.tree.AsyncTreeNode({
                id:'-1',                      //节点id
                text: '部门',                  //节点名称
                draggable:true,                  //是否支持拖动
                nodeType: 'async', 
                leaf:false
        });
           
	    	  
            treeDept_edit.setRootNode(rootDept_edit);
            comboWithTooltipDept_edit.on('expand',function(){  
            treeDept_edit.render('treeDept_edit');  
            //加载所有的子节点
            //tree.expandAll(); 
            });  
            
            
            treeDept_edit.on("click",function(node,event)
        {
            deptid = node.id;// 添加id到数组
            depttext = node.text;// 添加text到数组
            comboWithTooltipDept_edit.setValue(depttext); 
            var form = editUserFormPanel.getForm();
            form.findField('deptname').setValue(depttext);
            comboWithTooltipDept_edit.collapse();  
        });

		 	// 编辑用户Form
			var editUserFormPanel = new Ext.FormPanel({
				renderTo: 'editUserFormDiv',
		        labelWidth: 50,
		        border: false,
		        bodyStyle: 'background-color:transparent;',
		        url: '${pageContext.request.contextPath}/updateUserInfor.action',
		        items: [{
		            layout:'form',
		            border: false,
		            bodyStyle: 'padding:5px;background-color:transparent;',
		            items:[{// 账号
			                xtype:'textfield',
			                id: 'userName',
			                name: 'userName',
			                fieldLabel: '账号',
			                readOnly: true,
			                anchor:'95%',
			                allowBlank:false,
			                maxLength: 20
			           },{// 隐藏勾选角色名的值
			               xtype:'hidden',
			               id:'rolename',
			               name: 'rolename'
			           },{// 隐藏点击部门名的值
			               xtype:'hidden',
			               id:'deptname',
			               name: 'deptname'
			             // 角色
			           },comboWithTooltipRole_edit
			             // 部门
			            ,comboWithTooltipDept_edit
			           ,{// 姓名
			               xtype:'textfield',
			               id:'name',
			               name:'name',
			               fieldLabel:'姓名',
			               anchor:'95%',
			               allowBlank:false,
			               maxLength: 20
			           },{// 性别
			                xtype:'combo',
			                hiddenName: 'sex',
			                fieldLabel: '性别',
			                store: sexStore,
			                mode : 'local',
			                triggerAction: "all",
			                valueField: 'sex',
			                displayField: 'sexName',
			                editable: false,
		                    anchor:'95%'
		                },{// 生日
			                xtype:'datefield',
			                id: 'birthday',
			                name: 'birthday',
			                fieldLabel: '生日',
			                format: 'Y-m-d',
		                    anchor:'95%'
		                },{// 邮箱
			               xtype:'textfield',
			               id:'email',
			               name:'email',
			               fieldLabel:'邮箱',
			               anchor:'95%',
			               allowBlank:false,
			               vtype:'email',
			               maxLength: 50
			           },{// 电话
			               xtype:'textfield',
			               id:'mobile',
			               name:'mobile',
			               fieldLabel:'电话',
			               anchor:'95%',
			               maxLength: 20
			           },{// id
			               xtype:'hidden',
			               id:'id',
			               name: 'id'
			           },{// 操作类型
				           id:'buttonType',
			               xtype:'hidden',
			               name: 'type',
			               value: 'cancel'
			           }]
		        }]
		    });

		    // 发送请求-form初期化
			Anynote.ajaxRequest({
				baseUrl: '<%=baseUrl %>',
				baseParams: {userId:'<%=userId%>'},
				action: '${pageContext.request.contextPath}/getUserID.action',
				callback: function(jsonResult){
					var form = editUserFormPanel.getForm();
					form.setValues(jsonResult);
					comboWithTooltipRole_edit.setValue(jsonResult.rolename); 
					comboWithTooltipDept_edit.setValue(jsonResult.deptname); 
				}
			});

		    // 保存按钮
		    $("#editUser-save-button").click(function(){
			    var form = editUserFormPanel.getForm();
			    if(form.isValid()){
			        form.findField('rolename').setValue(checktext.toString());
			        form.findField('deptname').setValue(depttext);
			    	// 发送请求
					Anynote.ajaxRequest({
						baseUrl: '<%=baseUrl %>',
						baseParams: form.getValues(),
						action: '${pageContext.request.contextPath}/updateUserInfor.action?checkid='+checkid.toString()+'&deptid='+deptid,
						callback: function(jsonResult){
						    // location.href="${pageContext.request.contextPath}/page/index.jsp";
						    userListGrid.getStore().reload();
							editUserWindow.close();
						},
						showWaiting: true,
						failureMsg: '保存失败.'
					});
				}
			});
			
		 	// 取消按钮
		    $("#editUser-cancel-button").click(function(){
		    	editUserWindow.close();
			});
		});
	</script>
</head>
<body>
<div id="editUserDiv" style="width:100%;height:100%;">
	<div id="editUserToolBarDiv"></div>
	<div id="editUserFormDiv""></div>
</div>

</body>
</html>